﻿using System;
using System.Reflection;
using System.Runtime.Serialization;

namespace PmuRegistry.Data
{
	public class VersionConfigToNamespaceAssemblyObjectBinder : SerializationBinder
	{
		public override Type BindToType(string assemblyName, string typeName)
		{
			Type typeToDeserialize = null;

			try
			{
				string toAssemblyName = Assembly.GetExecutingAssembly().FullName.Split(new char[] { ',' })[0];
				Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

				foreach (Assembly assembly in assemblies)
				{
					if (assembly.FullName.Split(new char[] { ',' })[0] == toAssemblyName)
					{
						typeToDeserialize = assembly.GetType("PmuRegistry.Data.BusinessObjects.ConnectionSettings");
						break;
					}
				}
			}
			catch
			{
				return null;
			}

			return typeToDeserialize;
		}
	}
}
